package com.example.android.common.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.android.cardemulation.MainActivity;
import com.example.android.cardemulation.R;
import com.example.android.common.http.ThreadPoolUtils;
import com.example.android.common.logger.Log;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Joe on 2016/5/31.
 */
public class LoginActivity extends Activity {
    private static final String TAG = "LoginActivity";
    private static final int FLAG_LOGIN = 1;
    private static final int FLAG_REGIST= 2;
    private static final int FLAG_FORGET = 3;
    private EditText ETusername;
    private EditText ETpassword;
    Handler handler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what) {
                case FLAG_LOGIN:
                    finish();
                    String result = (String)msg.obj;
                    Log.d(TAG,result);
                    Toast.makeText(LoginActivity.this,LoginActivity.this.getResources().getString(R.string.notice_login_success),Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
            }
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        iniData();
    }

    private void iniData(){
        ETusername = (EditText) findViewById(R.id.username_login);
        ETpassword = (EditText) findViewById(R.id.password_login);
    }

    public void login(View v){
        final String username = ETusername.getText().toString();
        final String password = ETpassword.getText().toString();
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                String url = "http://192.168.6.86/l";
                // 第一步，创建HttpPost对象
                HttpPost httpPost = new HttpPost(url);
                httpPost.addHeader("User-Agent", "HCE_Android_IOS_APP");
                // 设置HTTP POST请求参数必须用NameValuePair对象
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));

                HttpResponse httpResponse = null;
                try {
                    // 设置httpPost请求参数
                    httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
                    httpResponse = new DefaultHttpClient().execute(httpPost);
                    //System.out.println(httpResponse.getStatusLine().getStatusCode());
                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        // 第三步，使用getEntity方法活得返回结果
                        String result = EntityUtils.toString(httpResponse.getEntity());
                        Message msg = Message.obtain();
                        msg.what = FLAG_LOGIN;
                        msg.obj = result;
                        handler.sendMessage(msg);
                        //Toast.makeText(LoginActivity.this,result,Toast.LENGTH_SHORT).show();
                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void regist(View v){

    }

    public void forget(View v){

    }
}